package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.handler.exception.CustomException;
import com.hhxy.model.dto.LinkDto;
import com.hhxy.service.LinkService;
import com.hhxy.utils.response.AppHttpCodeEnum;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author ghp
 * @date 2023/4/29
 * @title
 * @description
 */
@Api(tags = {"后端接口", "友链管理", "内容管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/content/link")
@Validated
public class BeLinkController {

    @Resource
    private LinkService linkService;

    @GetMapping("/list")
    @ApiOperation(value = "分页条件查询友链")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "name", value = "友链名"),
            @ApiImplicitParam(name = "status", value = "友链状态 0-审核通过 1-审核未通过 2-未审核")
    })
    public ResponseResult list(Integer pageNum, Integer pageSize, String name, String status) {
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return linkService.list(pageNum, pageSize, name, status);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据id删除友链")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "ids", value = "友链id", dataType = "Long[]")
    public ResponseResult deleteByIds(Integer pageNum, Integer pageSize, @PathVariable Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0){
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return linkService.deleteByIds(ids);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询友链")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "友链id", dataType = "Long")
    public ResponseResult getLinkById(Integer pageNum, Integer pageSize, @PathVariable Long id) {
        if (Objects.isNull(id)){
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return linkService.getLinkById(id);
    }

    @PutMapping
    @ApiOperation(value = "根据id修改友链")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "友链id", dataType = "Long")
    public ResponseResult updateLinkById(@RequestBody @Valid LinkDto linkDto) {
        return linkService.updateLinkById(linkDto);
    }

    @PostMapping
    @ApiOperation(value = "新增友链")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult add(@RequestBody @Valid LinkDto linkDto) {
        return linkService.add(linkDto);
    }





}
